package grobit.bencode

class BencoderTests extends GroovyTestCase {

    void testEncoding() {
        assert Bencoder.encode(54) == 'i54e'
        assert Bencoder.encode('hello') == '5:hello'
        assert Bencoder.encode(['alice', 'bob']) == 'l5:alice3:bobe'
        assert Bencoder.encode([ 'spam' : [ 'a','b']]) == 'd4:spaml1:a1:bee'
    }

    void testDecode() {
        assert Bencoder.decodeNext(asStream('4:spam')) == 'spam'
        assert Bencoder.decodeNext(asStream('i18e')) == 18
        assert Bencoder.decodeNext(asStream('l4:spam4:eggse')) == ['spam', 'eggs']
        assert Bencoder.decodeNext(asStream('d3:cow3:moo4:spam4:eggse')) == [ 'cow' : 'moo', 'spam' : 'eggs' ]
        assert Bencoder.decodeNext(asStream('d4:spaml1:a1:bee')) == [ 'spam' : [ 'a','b']]
    }

    private static InputStream asStream(String s) {
        return new ByteArrayInputStream(s.getBytes())
    }
}
